import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import numpy as np
from datetime import datetime as dt
print("Setup Complete")
Setup Complete
from itertools import cycle
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
dataset = pd.read_csv('C:/Users/rajat/Downloads/TSLA(16-21).csv')
dataset.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 1258 entries, 0 to 1257 Data columns (total 7 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Date 1258 non-null object 1 Open 1258 non-null float64 2 High 1258 non-null float64 3 Low 1258 non-null float64 4 Close 1258 non-null float64 5 Adj Close 1258 non-null float64 6 Volume 1258 non-null int64 dtypes: float64(5), int64(1), object(1) memory usage: 68.9+ KB
dataset['Date'] = pd.to_datetime(dataset['Date'])
dataset.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 1258 entries, 0 to 1257 Data columns (total 7 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Date 1258 non-null datetime64[ns] 1 Open 1258 non-null float64 2 High 1258 non-null float64 3 Low 1258 non-null float64 4 Close 1258 non-null float64 5 Adj Close 1258 non-null float64 6 Volume 1258 non-null int64 dtypes: datetime64[ns](1), float64(5), int64(1) memory usage: 68.9 KB
df2= dataset.groupby(dataset['Date'].dt.strftime('%B'))[['Open','Close']].mean()
new_order = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December']
df2 = df2.reindex(new_order, axis=0)
df2
| Open | Close | |
|---|---|---|
| Date | ||
| January | 211.862118 | 212.589568 |
| February | 227.558569 | 227.040441 |
| March | 194.185782 | 193.161236 |
| April | 205.595030 | 205.671611 |
| May | 181.159451 | 180.826095 |
| June | 204.369197 | 204.726543 |
| July | 226.404302 | 226.023074 |
| August | 173.212144 | 174.174936 |
| September | 129.064456 | 128.870416 |
| October | 129.469280 | 128.815189 |
| November | 137.678000 | 137.957262 |
| December | 183.599535 | 185.258544 |
fig = go.Figure()
fig.add_trace(go.Bar(
x=df2.index,
y=df2['Open'],
name='Stock Price HIGH',
marker_color='rgb(0, 153, 204)'
))
fig.add_trace(go.Bar(
x=df2.index,
y=df2['Close'],
name='Stock Price LOW',
marker_color='rgb(255, 128, 0)'
))
fig.update_layout(barmode='group',
title='Monthly mean of High and Low value of Stocks')
df3= dataset.groupby(dataset['Date'].dt.strftime('%B'))[['High']].max()
df3 = df3.reindex(new_order, axis=0)
df4= dataset.groupby(dataset['Date'].dt.strftime('%B'))[['Low']].min()
df4 = df4.reindex(new_order, axis=0)
new_order = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December']
fig1 = go.Figure()
fig1.add_trace(go.Scatter(
x=df3.index,
y=df3['High'],
mode = 'lines',
name='Max Stock',
marker_color='rgb(0, 153, 204)'
))
fig1.add_trace(go.Scatter(
x=df4.index,
y=df4['Low'],
mode = 'lines',
name='Min Stock',
marker_color='rgb(255, 128, 0)'
))
fig1.update_layout(
height=800,
showlegend=True,
title_text="Monthly Max and Min Stock trend",
)
fig1.show()
df = dataset.drop('Volume', axis=1)
def plot_line(dataset,title):
fig = px.line(dataset, x= "Date", y=dataset.columns,
title=title)
fig.update_xaxes(
dtick="M1",
tickformat="%b\n%Y")
fig.show()
dfm1 = df[df['Date']<'2017-08-16']
plot_line(dfm1,'Stock variation in first recorded yr(2016-08-16 to 2017-08-16)')
dfm2 = df[(df['Date'] > '2017-08-16') & (df['Date'] < '2018-08-16')]
plot_line(dfm2,'Stock variation in second recorded yr(2017-08-16 to 2018-08-16)')
dfm3 = df[(df['Date'] > '2018-08-16') & (df['Date'] < '2019-08-16')]
plot_line(dfm3,'Stock variation in third recorded yr(2018-08-16 to 2019-08-16)')
dfm4 = df[(df['Date'] > '2019-08-16') & (df['Date'] < '2020-08-16')]
plot_line(dfm4,'Stock variation in fourth recorded yr(2019-08-16 to 2020-08-16)')
dfm5 = df[(df['Date'] > '2020-08-16') & (df['Date'] < '2021-08-13')]
plot_line(dfm5, 'Stock variation in fifth recorded yr(2020-08-16 to 2021-08-13)')
def plot_line2(dst, title):
fig = px.line(dst, x= "Date", y=dst.Volume,
title=title)
fig.update_xaxes(
dtick="M1",
tickformat="%b\n%Y")
fig.show()
dfv1 = dataset[dataset['Date']<'2017-08-16']
plot_line2(dfv1,'Volume variation in first recorded yr(2016-08-16 to 2017-08-16)')
dfv2 = dataset[(dataset['Date'] > '2017-08-16') & (dataset['Date'] < '2018-08-16')]
plot_line2(dfv2,'Volume variation in second recorded yr(2017-08-16 to 2018-08-16)')
dfv3 = dataset[(dataset['Date'] > '2018-08-16') & (dataset['Date'] < '2019-08-16')]
plot_line2(dfv3,'Volume variation in third recorded yr(2018-08-16 to 2019-08-16)')
dfv4 = dataset[(dataset['Date'] > '2019-08-16') & (dataset['Date'] < '2020-08-16')]
plot_line2(dfv4,'Volume variation in fourth recorded yr(2019-08-16 to 2020-08-16)')
dfv5 = dataset[(dataset['Date'] > '2020-08-16') & (dataset['Date'] < '2021-08-13')]
plot_line2(dfv5, 'Volume variation in fifth recorded yr(2020-08-16 to 2021-08-13)')
fig = px.line(dataset, x='Date', y=dataset['Close'].rolling(window=20).mean())
fig.show()
fig1 = px.line(dataset, x='Date', y=dataset['Volume'].rolling(window=20).mean())
fig1.show()